// ShapeTool.java
package exercise.jslide.jslide.view.tools;

import exercise.jslide.jslide.app.AppContext;
import exercise.jslide.jslide.command.AddShapeCommand;
import exercise.jslide.jslide.model.shapes.Shape;
import exercise.jslide.jslide.view.EditorView;
import javafx.scene.Cursor;

public class ShapeTool implements Tool {
    private AppContext appContext;
    private EditorView editorView;
    private Shape currentShape;
    private double startX, startY;
    private final Class<? extends Shape> shapeClass;

    public ShapeTool(EditorView editorView, Class<? extends Shape> shapeClass) {
        this.appContext = AppContext.getInstance();
        this.editorView = editorView;
        this.shapeClass = shapeClass;
    }

    @Override
    public void activate() {
        editorView.setCursor(Cursor.CROSSHAIR);
    }

    @Override
    public void deactivate() {
        editorView.setCursor(Cursor.DEFAULT);
        currentShape = null;
    }

    @Override
    public void handleMousePressed(double x, double y) {
        try {
            currentShape = shapeClass.getDeclaredConstructor().newInstance();
            startX = x;
            startY = y;
            currentShape.setX(startX);
            currentShape.setY(startY);

            appContext.getCurrentPresentation()
                    .getCurrentSlide()
                    .addElement(currentShape);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void handleMouseDragged(double x, double y) {
        if (currentShape != null) {
            currentShape.setCurrentX(x);
            currentShape.setCurrentY(y);

            double width = Math.abs(x - startX);
            double height = Math.abs(y - startY);
            // 计算宽度和高度
            currentShape.setWidth(width);
            currentShape.setHeight(height);

            //如果图形是圆形
            if (currentShape.getClass().getSimpleName().equals("Circle")) {
                double radius = Math.max(width, height) / 2;
                currentShape.setWidth(2 * radius);
                currentShape.setHeight(2 * radius);
                if (x >= startX && y <= startY) {
                    currentShape.setX(startX);
                    currentShape.setY(startY - 2 * radius);
                } else if (x < startX && y <= startY) {
                    currentShape.setY(startY - 2 * radius);
                    currentShape.setX(startX - 2 * radius);
                } else if (x < startX && y > startY) {
                    currentShape.setX(startX);
                    currentShape.setX(startX - 2 * radius);
                } else {
                    currentShape.setY(startY);
                    currentShape.setX(startX);
                }
            } else if (currentShape.getClass().getSimpleName().equals("Line")) {
                if (x >= startX && y <= startY) {
                    currentShape.setX(startX);
                    currentShape.setY(startY);
                    currentShape.setHeight(-height);
                } else if (x < startX && y <= startY) {
                    currentShape.setY(startY);
                    currentShape.setX(startX);
                    currentShape.setHeight(-height);
                    currentShape.setWidth(-width);
                } else if (x < startX && y > startY) {
                    currentShape.setY(startY);
                    currentShape.setX(startX);
                    currentShape.setWidth(-width);
                } else {
                    currentShape.setY(startY);
                    currentShape.setX(startX);
                }
            } else {
                if (x >= startX && y <= startY) {
                    currentShape.setX(startX);
                    currentShape.setY(startY - height);
                } else if (x < startX && y <= startY) {
                    currentShape.setY(startY - height);
                    currentShape.setX(startX - width);
                } else if (x < startX && y > startY) {
                    currentShape.setY(startY);
                    currentShape.setX(startX - width);
                } else {
                    currentShape.setY(startY);
                    currentShape.setX(startX);
                }
            }

            appContext.getMainView().updateView();
        }
    }


    @Override
    public void handleMouseReleased(double x, double y) {
        if (currentShape != null) {
            appContext.getMainView().updateView();
            AddShapeCommand addShapeCommand = new AddShapeCommand(appContext.getCurrentPresentation(), currentShape);
            appContext.getUndoManager().executeCommand(addShapeCommand);
            currentShape = null;
            // 可以自动切换回选择工具
            editorView.setCurrentTool(null);
        }
    }

}